package cn.terminus.web.customer.contractinfo.controller;

import cn.terminus.client.customer.analysis.param.AnalysisYearParam;
import cn.terminus.client.customer.contractinfo.param.*;
import cn.terminus.client.customer.contractinfo.result.ContractInfoResult;
import cn.terminus.client.customer.contractinfo.result.RentContractInfoDetailResult;
import cn.terminus.client.customer.contractinfo.result.SellContractInfoDetailResult;
import cn.terminus.client.customer.contractinfo.service.ContractInfoService;
import cn.terminus.result.common.exception.ServiceException;
import cn.terminus.result.common.page.TerminusPageAble;
import cn.terminus.result.common.page.TerminusPageInfo;
import cn.terminus.result.common.result.TerminusResult;
import cn.terminus.result.common.util.BeanCopyUtils;
import cn.terminus.web.customer.analysis.AnalysisContractYearQuery;
import cn.terminus.web.customer.common.AbstractTerminusController;
import cn.terminus.web.customer.contractinfo.form.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 合同信息表(ContractInfo)控制层
 *
 * @author makejava
 * @version 1.0.0
 * @apiDefine ContractInfo 合同信息
 * @since 2021-07-21 11:01:08
 */
@RestController
@RequestMapping("/customer/contractInfo")
@Api(value = "合同管理核心表", tags = "合同管理核心表")
public class ContractInfoController extends AbstractTerminusController {
    /**
     * 服务对象
     */
    @Resource
    private ContractInfoService contractInfoService;

    @RequestMapping(value = "/addRentContract", method = RequestMethod.POST)
    @ApiOperation(value = "租赁合同信息新增", httpMethod = "POST")
    public TerminusResult<String> addRentContract(@RequestBody RentContractNewForm form) {
        RentContractNewParam param = BeanCopyUtils.copyProperties(form, RentContractNewParam.class);
        try {
            return contractInfoService.addRentContract(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("租赁合同信息新增失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("租赁合同信息新增失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

    @GetMapping(value = "/getCode")
    @ApiOperation(value = "生成合同编码", httpMethod = "GET")
    public TerminusResult<String> getCode(@RequestParam Integer type) {
        try {
            return contractInfoService.getCode(type, this.getTerminusContext());
        } catch (Exception e) {
            logger.error("生成合同编码失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }


    @RequestMapping(value = "/addSellContract", method = RequestMethod.POST)
    @ApiOperation(value = "销售合同信息新增", httpMethod = "POST")
    public TerminusResult<String> addSellContract(@RequestBody SellContractNewForm form) {
        SellContractNewParam param = BeanCopyUtils.copyProperties(form, SellContractNewParam.class);
        try {
            return contractInfoService.addSellContract(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("销售合同信息新增失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("销售合同信息新增失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }


    @RequestMapping(value = "/editRentContract", method = RequestMethod.POST)
    @ApiOperation(value = "租赁合同信息编辑", httpMethod = "POST")
    public TerminusResult<Boolean> editRentContract(@RequestBody RentContractInfoEditForm form) {
        RentContractNewParam param = BeanCopyUtils.copyProperties(form, RentContractNewParam.class);
        try {
            return contractInfoService.editRentContract(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("修改合同信息失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("修改合同信息失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

    @RequestMapping(value = "/editSellContract", method = RequestMethod.POST)
    @ApiOperation(value = "销售合同信息编辑", httpMethod = "POST")
    public TerminusResult<Boolean> editSellContract(@RequestBody SellContractInfoEditFrom form) {
        SellContractNewParam param = BeanCopyUtils.copyProperties(form, SellContractNewParam.class);
        try {
            return contractInfoService.editSellContract(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("修改合同信息失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("修改合同信息失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

    @PostMapping(value = "/delete")
    @ApiOperation(value = "合同信息删除", httpMethod = "POST")
    public TerminusResult<Boolean> delete(@RequestBody ContractInfoDeleteForm form) {
        ContractInfoDeleteParam param = BeanCopyUtils.copyProperties(form, ContractInfoDeleteParam.class);
        try {
            return contractInfoService.deleteByIds(param, this.getTerminusContext());

        } catch (ServiceException e) {
            logger.error("删除合同信息失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("删除合同信息失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

    @GetMapping(value = "/querySellContractById")
    @ApiOperation(value = "根据id查询销售合同详情", httpMethod = "GET")
    public TerminusResult<SellContractInfoDetailResult> querySellById(@RequestParam Long id) {
        try {
            return contractInfoService.querySellById(id, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("根据id 查询合同详情失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("根据id 查询合同详情失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

    @GetMapping(value = "/queryRentContractById")
    @ApiOperation(value = "根据id查询租赁合同详情", httpMethod = "GET")
    public TerminusResult<RentContractInfoDetailResult> queryRentById(@RequestParam Long id) {
        try {
            return contractInfoService.queryRentById(id, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("根据id 查询合同详情失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("根据id 查询合同详情失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiOperation(value = "合同信息分页查询", httpMethod = "POST")
    public TerminusResult<TerminusPageAble<ContractInfoResult>> page(@RequestBody ContractInfoQueryForm form) {
        ContractInfoQueryParam param = BeanCopyUtils.copyProperties(form, ContractInfoQueryParam.class);
        try {
            TerminusPageInfo page = new TerminusPageInfo(form.getCurrentPage(), form.getRowsOfPage());
            return contractInfoService.page(param, page, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("分页查询合同信息失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("分页查询合同信息失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

    @GetMapping(value = "/queryRoomsWithContract")
    @ApiOperation(value = "根据房间ids查询是否在合同中绑定", httpMethod = "GET")
    public TerminusResult<Boolean> queryRoomsWithContract(@RequestParam("ids") List<Long> ids) {
        try {
            return contractInfoService.queryRoomsWithContract(ids, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("根据房间ids查询是否在合同中绑定失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("根据房间ids查询是否在合同中绑定失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

    @PostMapping(value = "/analysisTotalContract")
    @ApiOperation(value = "统计合同总量", httpMethod = "POST")
    public TerminusResult<Integer> analysisTotalContract(@RequestBody AnalysisContractYearQuery form) {
        AnalysisYearParam param = BeanCopyUtils.copyProperties(form, AnalysisYearParam.class);
        try {
            return contractInfoService.analysisTotalContract(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("统计合同失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("统计合同失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }


    @PostMapping(value = "/getContractRoomsInfo")
    @ApiOperation(value = "获取项目下房号在合同中是否已经体现", httpMethod = "POST")
    public TerminusResult<Boolean> getContractRoomsInfo(@RequestBody ContractRoomsInfoQueryForm form) {
        ContractRoomsInfoQueryParam param = BeanCopyUtils.copyProperties(form, ContractRoomsInfoQueryParam.class);
        try {
            return contractInfoService.getContractRoomsInfo(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("获取项目下房号在合同中是否已经体现失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("获取项目下房号在合同中是否已经体现失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }
}
